﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Galaktika.BI.Silverlight.ClientApp.Web.Commands
{
    using Galaktika.BI.Runtime.Services;
    using Galaktika.BI.Silverlight.ClientApp.Web.Runtime;
    using Galaktika.BI.Silverlight.ClientApp.Web.DAL;
    using Galaktika.BI.Silverlight.ClientApp.Web.Runtime.Services;

    public class ResetContextCommand : ServerCommand
    {
        public ResetContextCommand(InvokeSchema invoker)
            : base(Galaktika.BI.Silverlight.Commands.CommandId.ResetContextCommandId)
        {
            this.ResetContext = invoker[KnownInvokeArgs.ResetContext];

            if (this.ResetContext == null)
            {
                throw new ArgumentNullException(KnownInvokeArgs.ContextId);
            }
        }

        public string ResetContext { get; private set; }

        public override InvokeResultSchema Invoke()
        {
            InvokeResultSchema res = new InvokeResultSchema(base.Id);
            DataBinder binder = DataBinder.Current;

            IBindingContext context = binder.GetContext(this.ResetContext);
            VariablesContainer v = binder.GetVariables(context);
            v.Clear();

            context.IsDirty = true;
            context.Reset();
            context.Refresh();
            binder.Reset(this.ResetContext);   

            res.ContentType = KnownContentTypes.Empty;
            
            return res;
        }
    }
}